package com.system.controller;

import com.system.po.*;
import com.system.service.AppealService;
import com.system.service.CourseService;
import com.system.service.StudentService;
import com.system.service.TeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "申诉")
@Controller
@RequestMapping("/wxappeal")
public class WxAppealController {
    @Resource(name = "teacherServiceImpl")
    private TeacherService teacherService;

    @Resource(name = "courseServiceImpl")
    private CourseService courseService;

    @Resource(name = "appealServiceImpl")
    private AppealService appealService;

    @Resource(name = "studentServiceImpl")
    private StudentService studentService;

    @ApiOperation(value = "申诉列表")
    @ApiImplicitParams({
    })
    @RequestMapping(value = "/wxShowAppeal", method = {RequestMethod.GET})
    @ResponseBody
    public Map<String, Object> wxShowAppeal(Appeal appeal) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();

        List<Appeal> list = appealService.selectList(appeal);
        for (Appeal app: list) {
            CourseCustom course = courseService.findById(app.getCourseid());
            if (course != null) {
                app.setCourseName(course.getCoursename());
            }
            Teacher teacher = teacherService.findById(app.getTeacherid());
            if (teacher != null) {
                app.setTeacherName(teacher.getUsername());
            }
            Student student = studentService.findById(app.getStudentid());
            if (student != null) {
                app.setStudentName(student.getUsername());
            }
        }
        map.put("appealList", list);
        return map;
    }

    @ApiOperation(value = "添加申诉记录")
    @ApiImplicitParams({
    })
    @RequestMapping(value = "/saveAppeal", method = {RequestMethod.POST})
    @ResponseBody
    public Object saveAppeal(@RequestBody Appeal appeal) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();



        //通过courseId获取teacherId
        Integer courseid = appeal.getCourseid();
        CourseCustom byId = courseService.findById(courseid);
        if (byId != null) {
            appeal.setTeacherid(byId.getTeacherid());
        }
        //添加之前判断是否已经存在审核数据
        appealService.del(appeal.getStudentid(),appeal.getCourseid());


        return appealService.insert(appeal);
    }

    @ApiOperation(value = "修改申诉记录")
    @ApiImplicitParams({
    })
    @RequestMapping(value = "/updateAppeal", method = {RequestMethod.POST})
    @ResponseBody
    public Object updateAppeal(Appeal appeal)
    {

        return appealService.update(appeal);
    }

    @ApiOperation(value = "删除申诉记录")
    @ApiImplicitParams({
    })
    @RequestMapping(value = "/deleteAppeal", method = {RequestMethod.GET})
    @ResponseBody
    public Object deleteAppeal(Integer id)
    {
        return appealService.deleteById(id);
    }

    //获取单条记录
    @RequestMapping(value = "/getById", method = {RequestMethod.GET})
    @ResponseBody
    @ApiOperation("获取单个记录")
    public Map getById(Integer id) {
        return appealService.getById(id);
    }

}
